import lightning as L
from torch.utils.data import DataLoader
from omegaconf import DictConfig

from ecgcmr.imaging.img_dataset.DownstreamImageDataset import DownstreamImageDataset


class DownstreamImageDataModule(L.LightningDataModule):
    def __init__(
        self,
        cfg: DictConfig,
        mask_labels: bool,
        supervised: bool = False,
        ) -> None:
        super().__init__()

        self.cfg = cfg
        self.batch_size = cfg.downstream_task.batch_size
        self.num_workers = cfg.downstream_task.num_workers
        self.mask_labels = mask_labels
        self.supervised = supervised

    def setup(self, stage: str):
        if stage == 'fit':
            self.dataset_train = DownstreamImageDataset(cfg=self.cfg, mode='train', mask_labels=self.mask_labels, supervised=self.supervised, apply_augmentations=False)
            self.dataset_val = DownstreamImageDataset(cfg=self.cfg, mode='val', mask_labels=self.mask_labels, supervised=self.supervised, apply_augmentations=False)

    def train_dataloader(self) -> DataLoader:
        return DataLoader(
            self.dataset_train, 
            batch_size=self.batch_size,
            shuffle=True, 
            num_workers=self.num_workers,
            pin_memory=True
        )

    def val_dataloader(self) -> DataLoader:
        return DataLoader(
            self.dataset_val, 
            batch_size=self.batch_size, 
            shuffle=False, 
            num_workers=self.num_workers,
            pin_memory=True
        )
